﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Studentium.Entities;

namespace Studentium.UIElements
{
    public partial class EditPayment : Form
    {
        private Payment payment;
        private bool newCourse;
        private StudentInfoTabs studentInfos;

        public EditPayment(Payment payment, StudentInfoTabs studentInfos, bool newCourse)
        {
            InitializeComponent();

            this.studentInfos = studentInfos;
            this.newCourse = newCourse;
            this.payment = payment;
            startWeekBox.Text = payment.WeekStarted.ToString();
            if (payment.StartDate <= DateTime.MinValue || payment.StartDate >= DateTime.MaxValue)
                payment.StartDate = DateTime.Now;
            startDatePicker.Value = payment.StartDate;
            if (payment.FinishDate <= DateTime.MinValue || payment.FinishDate >= DateTime.MaxValue)
                payment.FinishDate = DateTime.Now;
            finishDatePicker.Value = payment.FinishDate;
            moneyPaidBox.Text = payment.MoneyPaid.ToString();
            discountBox.Text = payment.Discount.ToString();
            discountRefBox.Text = payment.DiscountReferer;
            commentBox.Text = payment.Comment;
            priceBox.Text = payment.Price.ToString();
            classesNumBox.Text = payment.ClassesNumber.ToString();
            bookTypeBox.DataSource = Enum.GetValues(typeof (BookType));
            bookTypeBox.SelectedItem = payment.BookType;

            bookLevelBox.DataSource = Enum.GetValues(typeof (StudentLevel));
            bookLevelBox.SelectedItem = payment.BookLevel;

            if (payment.IsCoursePayment)
                coursePayentRad.Checked = true;
            else
            if (payment.IsBookPayment)
                bookRad.Checked = true;
            else
                otherPaymentRad.Checked = true;

            coursePayentRad_CheckedChanged(coursePayentRad, null);
        }

        private void saveStudenCourseBtn_Click(object sender, EventArgs e)
        {
            int weekStarted = 0;
            if(coursePayentRad.Checked && 
                (!Int32.TryParse(startWeekBox.Text, out weekStarted) || weekStarted<1 || weekStarted > 60) )
            {
                MessageBox.Show("Start week should be a number between 1 and 60", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            int moneyPaid;
            if (!Int32.TryParse(moneyPaidBox.Text, out moneyPaid) || moneyPaid < 0)
            {
                MessageBox.Show("Money paid should be a positive number ", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            int discount = 0;
            if (discountBox.Text.Trim().Length > 0 && (!Int32.TryParse(discountBox.Text, out discount) || discount < 0))
            {
                MessageBox.Show("Discount should be a positive number", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            int price;
            if (!Int32.TryParse(priceBox.Text, out price) || price < 0)
            {
                MessageBox.Show("Price should be a positive number ", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            int classesNum = 0;
            if (classesNumBox.Text.Trim().Length > 0 && (!Int32.TryParse(classesNumBox.Text, out classesNum) || classesNum < 0))
            {
                MessageBox.Show("Classes number should be a positive number", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            
            if (price < discount)
            {
                MessageBox.Show("Price cann't be less than discount", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }

            if(!coursePayentRad.Checked)
            {
                finishDatePicker.Value = startDatePicker.Value;
            }

            if (startDatePicker.Value > finishDatePicker.Value)
            {
                MessageBox.Show("Start date cann't be greater then finish one", "Error", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            payment.WeekStarted = weekStarted;
            payment.StartDate = startDatePicker.Value;
            payment.FinishDate = finishDatePicker.Value;
            payment.MoneyPaid = moneyPaid;
            payment.Discount = discount;
            payment.DiscountReferer = discountRefBox.Text;
            payment.Price = price;
            payment.ClassesNumber = classesNum;
            payment.Comment = commentBox.Text;
            payment.IsCoursePayment = coursePayentRad.Checked;
            payment.IsBookPayment = bookRad.Checked;
            payment.BookType = (BookType)bookTypeBox.SelectedValue;
            payment.BookLevel = (StudentLevel) bookLevelBox.SelectedValue;
            if (newCourse)
                studentInfos.SaveNewStudentItem(payment);
            else
                studentInfos.UpdateStudentItem(payment);
                
            this.Close();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void coursePayentRad_CheckedChanged(object sender, EventArgs e)
        {
            if(((RadioButton)sender).Checked)
            {
                finishDateLbl.Visible = true;
                finishDatePicker.Visible = true;
                startWeekLbl.Visible = true;
                startWeekBox.Visible = true;
                startDateLbl.Text = "Course start date:";
                classesNumBox.Visible = true;
                classesNumLbl.Visible = true;
            }
            else
            {                
                finishDateLbl.Visible = false;
                finishDatePicker.Visible = false;
                startWeekLbl.Visible = false;
                startWeekBox.Visible = false;
                startDateLbl.Text = "Date:";
                classesNumBox.Visible = false;
                classesNumLbl.Visible = false;
            }
        }

        private void bookRad_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
            {
                bookTypeLbl.Visible = true;
                bookTypeBox.Visible = true;
                bookLevelBox.Visible = true;
                bookLevelLbl.Visible = true;
            }
            else
            {
                bookTypeLbl.Visible = false;
                bookTypeBox.Visible = false;
                bookLevelBox.Visible = false;
                bookLevelLbl.Visible = false;
            }
        }

        private void otherPaymentRad_CheckedChanged(object sender, EventArgs e)
        {
        }        
    }
}
